package com.aifast.dict.demo;

import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.springframework.beans.factory.annotation.Value;
import java.time.Duration;

/**
 * @Author AiFast
 * @since 2022/12/27 17:16
 */
public class DemoService {

    @Value("${ai.dict.enable}")
    private boolean enable;

    public void run(){
        if(!enable){
            System.out.println("请先开启FastDict！！！");
        }
        System.out.println("成功调用到该starter");

    }

    public static void main(String[] args) {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .with(CacheManagerBuilder.persistence("d:\\myCache\\"))
                .build(true);
        ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
                .heap(1, MemoryUnit.MB)
                .disk(10, MemoryUnit.GB, true)
                .build();

        CacheConfiguration<Integer, String> cacheConfiguration =
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resourcePools)
                        .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(10)))
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(5)))
                        .build();

        Cache<Integer, String> aifast = cacheManager.createCache("aifast", cacheConfiguration);


        aifast.put(392,"dsfsdfssghdjkfkdjdshsgsbdnfbmvdsdwret3456543");

        Console.log("取到的值：");
        Console.log(aifast.get(392));
        ThreadUtil.sleep(6000);
        Console.log(aifast.get(392));



    }

}
